﻿using System;
using System.Collections;
using System.Collections.Generic;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.View.Base;
using System.ComponentModel;
using System.Windows.Forms;

namespace HR60_PanelSoft.View.TabForms.ListItems
{
    //GC: [g1]优化：将核心的基础控件传递到外层[ButtonItem]
	public partial class ButtonItem : BaseFormControl
	{
		public ButtonItem()
		{
			InitializeComponent();

			//selDevice.ValueChanged += new EventHandler(selDevice_ValueChanged);
			//selScene.ValueChanged += new EventHandler(selScene_ValueChanged);
            selDevice.ValueChanged += new ValueChangedHandler(selDevice_ValueChanged);
            selScene.ValueChanged += new ValueChangedHandler(selScene_ValueChanged);
		}

		[Browsable(true)]
		[Description("标题"), Category("String"), DefaultValue("标题")]
		public string Title
		{
			get
			{
				return ltlId.Text;
			}
			set
			{
				ltlId.Text = value;
			}
		}

		/// <summary>
		/// 绑定按钮数据
		/// </summary>
		/// <param name="btn"></param>
		public void bindButton(PanelButton btn)
		{
			// 不触发选择事件
			this.CatchValueChange = false;
			selGroup.CatchValueChange = false;
			selDevice.CatchValueChange = false;
			selScene.CatchValueChange = false;
			
			// 不选择按钮状态
			if (btn == null)
			{
				selGroup.cleanItems();
				selDevice.cleanItems();
				selScene.cleanItems();
				ltlId.Text = "未选择";
				this.Enabled = false;
				return;
			}

			// 查找已关联的设备
			Device device = findDeviceByUuid(btn.mappingDevice);
			DeviceGroup group = findGroupByDeviceUuid(btn.mappingDevice);

			// 查找已关联的场景
			Scene scene = findSceneByUuid(btn.mappingScene);

			// 设备分组
			selGroup.setStaticFirstItem("选择设备分组");
			selGroup.bindData((IList)deviceGroups, "name");
			if (group != null) selGroup.Value = group;

			// 设备
			selDevice.setStaticFirstItem("选择设备");
			refreshDeviceList(group);
			if (device != null) selDevice.Value = device;

			// 场景
			selScene.setStaticFirstItem("选择场景");
			selScene.bindData((IList)scenes, "name");
			if (scene != null) selScene.Value = scene;

			this.Enabled = true;
			this.CatchValueChange = true;
			selGroup.CatchValueChange = true;
			selDevice.CatchValueChange = true;
			selScene.CatchValueChange = true;
		}

		// 联动设备列表
		private void refreshDeviceList(DeviceGroup group)
		{
			IList<Device> list = new List<Device>();
			if (group != null)
			{
				foreach (Device dev in group.devices)
				{
					if(dev.type.canButtonCtrl)
					{
						list.Add(dev);
					}
				}
			}
			selDevice.bindData((IList)list, "nickName");
		}

		// 选择分组
		private void selGroup_ValueChanged(object sender, ValueChangedEventArgs e)
		{
			selDevice.CatchValueChange = false;
			refreshDeviceList((DeviceGroup)selGroup.Value);
			selDevice.CatchValueChange = true;
			valueChange(this, e);
		}

		// 选择设备
        void selDevice_ValueChanged(object sender, ValueChangedEventArgs e)
		{
            selScene.CatchValueChange = false;
            selScene.Value = null;
            selScene.CatchValueChange = true;

			valueChange(this, e); /* 这里并没有将关联关系保存到内存对象 */

		}

		// 选择场景
        void selScene_ValueChanged(object sender, ValueChangedEventArgs e)
		{
            selDevice.CatchValueChange = false;
            selDevice.Value = null;
            selDevice.CatchValueChange = true;
			valueChange(this, e);

		}

		// 重写Value属性
		public override object Value
		{
			get
			{
				if (selDevice.Value != null)
				{
					return selDevice.Value;
				}
				if (selScene.Value != null)
				{
					return selScene.Value;
				}
				return null;
			}
			set
			{
				if (value == null)
				{
					selDevice.Value = null;
					selScene.Value = null;
					return;
				}
				if(value.GetType().Equals(typeof(Device)))
				{
					selDevice.Value = value;
					return;
				}
				if (value.GetType().Equals(typeof(Scene)))
				{
					selScene.Value = value;
					return;
				}
			}
		}

        /* [g1.s1] */

        public override List<Control> GetCoreControls()
        {
            List<Control> rst = new List<Control>();

            rst.Clear();
            rst.Add(selGroup);
            rst.Add(selDevice);
            rst.Add(selScene);
            rst.AddRange(selDevice.GetCoreControls());
            rst.AddRange(selScene.GetCoreControls());
            rst.AddRange(selGroup.GetCoreControls());

            return rst;
        }

        /* end of [g1.s1] */

	}
}
